package net.rlair.sbdemo.service;

import net.rlair.sbdemo.dao.jpa.UserRepository;
import net.rlair.sbdemo.dao.mybatis.bootmapper.UserMapper;
import net.rlair.sbdemo.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserRepository userRepository;

    public Page<User> userPageWithMybatis(final Pageable page) {
        return new PageImpl<>(userMapper.selectPage(page), page, userMapper.countUser());
    }

    public Page<User> userPageWithJpa(final Pageable page) {
        return userRepository.findAll(page);
    }

    public int saveWithMybatis(final User user) {
        return userMapper.addUser(user);
    }

    public int saveWithJpa(final User user) {
        userRepository.save(user);
        return 1;
    }
}
